package com.zdzx.controller.admin.content;

import java.time.LocalDateTime;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdzx.base.common.util.RedisUtil;
import com.zdzx.base.common.util.UserInfoUtil;
import com.zdzx.common.result.PageInfo;
import com.zdzx.common.result.PageUtil;
import com.zdzx.common.result.Result;
import com.zdzx.common.result.ResultCode;
import com.zdzx.common.result.ResultUtil;
import com.zdzx.mapper.ArticleMapper;
import com.zdzx.model.service.base.result.UserInfo;
import com.zdzx.model.service.content.vo.Article;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(tags = "文章")
@RestController
@RequestMapping("/admin")
public class ArticleController {

	@Autowired
	private ArticleMapper articleMapper;
	@Autowired
	private RedisUtil redisUtil;

	@ApiOperation(value = "获取列表", notes = "")
	@GetMapping("/article")
	public Result<PageInfo<Article>> list(
			@ApiParam("") @RequestParam(value = "pageNum", required = false) Integer pageNum,
			@ApiParam("") @RequestParam(value = "pageSize", required = false) Integer pageSize,
			@ApiParam("") @RequestParam(value = "title", required = false) String title,
			@ApiParam("") @RequestParam(value = "tenantId", required = false) String tenantId,
			@ApiParam("") @RequestParam(value = "type", required = false) String type,
			@ApiParam("法律法规类型") @RequestParam(value = "flfgType", required = false) String flfgType,
			@ApiParam("地方性法规类型") @RequestParam(value = "dfxfgType", required = false) String dfxfgType,
			@ApiParam("规范性文件类型") @RequestParam(value = "gfxwjfType", required = false) String gfxwjfType,
			@ApiParam("") @RequestParam(value = "classify", required = false) String classify,HttpServletRequest request) {
		UserInfo loginInfo = UserInfoUtil.getLoginInfo(UserInfo.class, redisUtil, request);
		if (loginInfo == null) {
			return ResultUtil.data(ResultCode.NOT_LOGIN.getCode(), ResultCode.NOT_LOGIN.getMes(), null);
		}
		Page<Article> page = new Page<>(pageNum == null ? 1 : pageNum, pageSize == null ? 10 : pageSize);
		QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
		queryWrapper.select("id","title","title_image","`desc`","author","views","`type`","tenant_id","gfxwjf_type","flfg_type","dfxfg_type","create_time","update_time");
		queryWrapper.orderByDesc("create_time");
		if (!StringUtils.isEmpty(title)) {
			queryWrapper.like("title", title);
		}
		if (!StringUtils.isEmpty(tenantId)) {
			queryWrapper.eq("tenant_id", tenantId);
		}
		if (!StringUtils.isEmpty(type) && !"0".equals(type)) {
			queryWrapper.eq("type", type);
		}
		
		if (!StringUtils.isEmpty(type) && !"0".equals(type)) {
			if(type.equals("dabz")) {
				
			}else if(type.equals("flfg")) {
				if(!StringUtils.isEmpty(dfxfgType) &&flfgType.equals("dfxfg")) {
					queryWrapper.eq("dfxfg_type", dfxfgType);
				}else if(!StringUtils.isEmpty(gfxwjfType) &&flfgType.equals("gfxwjf")){
					queryWrapper.eq("gfxwjf_type", gfxwjfType);
				}else {
					if(!StringUtils.isEmpty(flfgType)) {
						queryWrapper.eq("flfg_type", flfgType);
					}
				}
			}
		}
		
		queryWrapper.eq("tenant_id", loginInfo.getTenantId());
		IPage<Article> info = articleMapper.selectPage(page, queryWrapper);
		PageInfo<Article> data = PageUtil.data(info.getPages(), info.getCurrent(), info.getTotal(), info.getRecords());
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, data);
	}
	
	@ApiOperation(value = "获取列表", notes = "")
	@GetMapping("/article/{id}")
	public Result<Article> getArticle(@PathVariable("id")String id) {
		Article info = articleMapper.selectById(id);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null,info);
	}

	@ApiOperation(value = "添加", notes = "")
	@PostMapping("/article")
	public Result<String> save(@RequestBody Article entity,HttpServletRequest request) {
		UserInfo loginInfo = UserInfoUtil.getLoginInfo(UserInfo.class, redisUtil, request);
		if (loginInfo == null) {
			return ResultUtil.data(ResultCode.NOT_LOGIN.getCode(), ResultCode.NOT_LOGIN.getMes(), null);
		}
		if(StringUtils.isEmpty(entity.getContent())) {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(),"文章内容不能为空", null);
		}
		int result = 0;
		// entity.setTitleImage(entity.getTitleImage().replace(SystemConfig.ossIp,
		// ""),false);
		if (StringUtils.isEmpty(entity.getFree())) {
			entity.setFree("1");
		}
		entity.setTenantId(loginInfo.getTenantId());
		entity.setViews(0);
		entity.setCreateTime(LocalDateTime.now());
		entity.setUpdateTime(LocalDateTime.now());
		String title = entity.getTitle();
		if (title.startsWith("GB"))
			entity.setStandardType("1");
		else
			entity.setStandardType("0");
		
		result = articleMapper.insert(entity);

		if (result > 0) {
			return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, entity.getId());
		} else {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), ResultCode.RESULT_ERROR.getMes(), null);
		}
	}

	@ApiOperation(value = "修改", notes = "")
	@PutMapping("/article")
	public Result<String> edit(@RequestBody Article entity,HttpServletRequest request) {
		UserInfo loginInfo = UserInfoUtil.getLoginInfo(UserInfo.class, redisUtil, request);
		if (loginInfo == null) {
			return ResultUtil.data(ResultCode.NOT_LOGIN.getCode(), ResultCode.NOT_LOGIN.getMes(), null);
		}
		if(StringUtils.isEmpty(entity.getContent())) {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(),"文章内容不能为空", null);
		}
		int result = 0;
		if (StringUtils.isEmpty(entity.getFree())) {
			entity.setFree("1");
		}
		String title = entity.getTitle();
		if (title.startsWith("GB"))
			entity.setStandardType("1");
		else
			entity.setStandardType("0");
		entity.setViews(null);
		entity.setTenantId(loginInfo.getTenantId());
		entity.setUpdateTime(LocalDateTime.now());
		result = articleMapper.updateById(entity);

		if (result > 0) {
			return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, null);
		} else {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), ResultCode.RESULT_ERROR.getMes(), null);
		}
	}

	@ApiOperation(value = "删除", notes = "")
	@DeleteMapping("/article/{articleId}")
	public Result<String> del(@PathVariable("articleId") String articleId) {
		int result = articleMapper.deleteById(articleId);
		if (result > 0) {
			return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, null);
		} else {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), ResultCode.RESULT_ERROR.getMes(), null);
		}
	}

	
}
